/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.doeacc.tms.web.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

import edu.doeacc.tms.domain.Customer;
import edu.doeacc.tms.domain.ShippingInvoice;
import edu.doeacc.tms.domain.ShippingInvoiceItem;
import edu.doeacc.tms.service.dao.CustomerFacade;
import edu.doeacc.tms.service.dao.ShippingInvoiceFacade;
import edu.doeacc.tms.web.util.JsfUtil;

/**
 *
 * @author slok
 */
@ManagedBean(name = "invoiceBean")
@ViewScoped
public class InvoiceBean implements Serializable {

    @EJB
    private transient ShippingInvoiceFacade invoiceFacade;
    @EJB
    private transient CustomerFacade customerFacade;
    private transient  ShippingInvoice current;
    private transient  ShippingInvoiceItem selectedInvoiceItem;
    @ManagedProperty("#{msgs}")
    private transient ResourceBundle bundle;
    private List<Customer> customers;

    DataModel<ShippingInvoiceItem> model;
    
    public void setBundle(ResourceBundle bundle) {
        this.bundle = bundle;
    }

    private ShippingInvoiceFacade getFacade() {
        return invoiceFacade;
    }

    public List<Customer> getCustomers() {
        if (customers == null) {
            customers = customerFacade.findAll();
        }
        return customers;
    }

    public ShippingInvoice getSelected() {
        if (current == null) {
            if (this.currentId > -1) {
                this.current = getFacade().find(this.currentId);
            } else {
                current = new ShippingInvoice();
            }
        }
        return current;
    }

    public void selectInvoiceItem(ShippingInvoiceItem selectedInvoiceItem) {
        this.selectedInvoiceItem = selectedInvoiceItem;
    }

    public ShippingInvoiceItem getSelectedInvoiceItem() {
        return this.selectedInvoiceItem;
    }

    public void setSelectedInvoiceItem(ShippingInvoiceItem selectedInvoiceItem) {
         this.selectedInvoiceItem = selectedInvoiceItem;
    }

    public String prepareEdit(Long id) {
        this.current = getFacade().find(id);
        return "pretty:editInvoice";
    }

    public void newInvoice() {
        if (this.selectedInvoiceItem == null) {
            this.current = new ShippingInvoice();
            newRow();
        }
    }

    public DataModel<ShippingInvoiceItem> getModel() {
        if (model == null) {
            model = new ListDataModel<ShippingInvoiceItem>(getSelected().getItems());
        }
        return model;
    }
    
    public void newRow() {
        this.selectedInvoiceItem = new ShippingInvoiceItem();
        this.selectedInvoiceItem.setId(new Random().nextLong());
        this.selectedInvoiceItem.setInvoice(this.current);
        this.current.getItems().add(selectedInvoiceItem);
        model = null;
    }

    public void deleteRow(ShippingInvoiceItem item) {
        this.current.getItems().remove(item);
        model = null;
    }
    
    public void deleteSelected() {
       List<ShippingInvoiceItem> selections = new ArrayList<ShippingInvoiceItem>();
       for(ShippingInvoiceItem item: this.current.getItems()) {
           if (item.isSelected())
                selections.add(item);
       }
       
       for(ShippingInvoiceItem item: selections) {
             this.current.getItems().remove(item);
       }
     
    }

    public String create() {
        try {
            getFacade().create(current);
            JsfUtil.addSuccessMessage("Invoice # '" + current.getId() +"' Created Successfully");
            return "pretty:addInvoice";
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, bundle.getString("PersistenceErrorOccured"));
            return null;
        }
    }

    public String update() {
        try {
            getFacade().edit(current);
            JsfUtil.addSuccessMessage("Invoice Updated Successfully");
            return "pretty:viewInvoice";
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, bundle.getString("PersistenceErrorOccured"));
            return null;
        }
    }

    public String prepareCreate() {
        newInvoice();
        return "pretty:addInvoice";
    }
//    public String destroy(Long id) {
//         try {
//            customerFacade.remove(customerFacade.find(id));
//            customers = null;
//            JsfUtil.addSuccessMessage("Customer deleted successfully");
//        } catch (Exception e) {
//            JsfUtil.addErrorMessage(e, bundle.getString("PersistenceErrorOccured"));
//        }
//        return "pretty:listCustomers";
//    }
    Long currentId = -1L;

    public Long getCurrentId() {
        return getSelected().getId();
    }

    public void setCurrentId(Long id) {
        this.currentId = id;
    }

    private TreeNode root;

    public TreeNode getInvoices() {
        if (root == null) {
            root = constructTree(getFacade().findAll());
        }
        return root;
    }

    private TreeNode constructTree(List<ShippingInvoice> invoices) {
        TreeNode rootNode = new DefaultTreeNode("root", null);
        for(ShippingInvoice invoice: invoices) {
            TreeNode invoiceNode = new DefaultTreeNode("invoice", invoice, rootNode);
            for(ShippingInvoiceItem item: invoice.getItems()) {
                 TreeNode itemNode = new DefaultTreeNode("item", item, invoiceNode);
            }
        }
        return rootNode;
    }
}
